﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MPev
{
    public partial class MPGroupControl : UserControl
    {
        public MPGroup group;

        public MPGroupControl()
        {
            InitializeComponent();
        }

        private void delBtn_Click(object sender, EventArgs e)
        {
            if (listBox.SelectedIndex >= 0)
            {
                //verbListBox.Items.RemoveAt(verbListBox.SelectedIndex);
                group.list.RemoveAt(listBox.SelectedIndex);
                syncListBox();
            }
        }
        private void addBtn_Click(object sender, EventArgs e)
        {
            InputClassForm icf = new InputClassForm();
            if (icf.ShowDialog() == DialogResult.OK)
            {
                icf.inputclass.type = group.type;
                group.list.Add(icf.inputclass);
                syncListBox();
            }
        }
        public void syncListBox()
        {
            listBox.Items.Clear();
            foreach (MPClass c in group.list)
            {
                string str = c.name;
                if (c.attr.Count() > 0)
                {
                    str += " [";
                    foreach (string a in c.attr)
                    {
                        str += a + ",";
                    }
                    str = str.Remove(str.Length - 1) + ']';
                }
                listBox.Items.Add(str);
            }
        }

        private void listBox_DoubleClick(object sender, EventArgs e)
        {
            if (listBox.SelectedIndex < 0) return;

            InputClassForm icf = new InputClassForm();
            int i = listBox.SelectedIndex;
            icf.textBox1.Text = group.list[i].name;
            icf.textBox2.Text = (group.list[i].attr.Count() >= 1) ? group.list[i].attr[0] : "";
            icf.textBox3.Text = (group.list[i].attr.Count() >= 2) ? group.list[i].attr[1] : "";
            icf.textBox4.Text = (group.list[i].attr.Count() >= 3) ? group.list[i].attr[2] : "";

            if (icf.ShowDialog() == DialogResult.OK)
            {
                icf.inputclass.type = group.type;
                group.list[i] = icf.inputclass;
                syncListBox();
            }
        }
    }
}
